#ifndef	APP_GLOBALS_RELAY
#define	APP_GLOBALS_RELAY
#include <includes.h>


static void PORLocalRAMReset_RELAY(void);
static void PORInputsStaReset_RELAY(void);
static void RelayOutputsLayerHandle(OS_FLAGS os_value);
static void RelayOutputsWorkLayerHandle(void);
static void PidControlWorkLayerHandle(void);
/**
  * @brief  Local RAM Reset for RELAY
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
static void PORLocalRAMReset_RELAY(void)
{
	
	
}

/**
  * @brief  Status Reset for NORM AD
  * @param  none
  * @return none.
  * @Caller POR
  * @Note   none.
  */
static void PORInputsStaReset_RELAY(void)
{

}

/* Relay Outpus */
static void RelayOutputsLayerHandle(OS_FLAGS os_value)
{
	if(os_value&(OSRelayFlags_UpdRequest|OSRelayFlags_UpdTiming))
	  RelayOutputsWorkLayerHandle();
}

static void PidControlWorkLayerHandle(void)
{

}

uint32_t RelayOutputSta1;
#define LOWPINOUT 
/**
 * @brief 
 * 逻辑输出 - 端口低电平
 * 逻辑清除 - 端口高电平
 */
static void RelayOutputsWorkLayerHandle(void)
{
    static uint32_t LastRelayOutputSta=0;
    uint32_t RelayActiveSta,RelayOutputSta,RelayAuxOrdBit=1;

    for (uint8_t i = 0; i < RelayOutputsTOTNum; i++)
    {
        if(GetFlagShareBufferDataContent(NULL,CfgOperaType_None,ShareBuffer_Outputs,i))//配置为输出
        {
            RelayOutputSta|=RelayAuxOrdBit<<i;
        }
        else
        {
            RelayOutputSta&=~(RelayAuxOrdBit<<i);
        }
    }
    
    RelayOutputSta1 = RelayOutputSta;

    if(RelayOutputSta==LastRelayOutputSta)
      return;

    /******************************************/
    /* Update Relay Outputs(MCU Ports) */
    /******************************************/
    uint32_t TempOutSta = RelayOutputSta;
    for (uint8_t i = 0; i < RelayOutputsTOTNum; i++)
    {
      #ifdef LOWPINOUT
        if (TempOutSta & DEF_BIT_00)
            GPIO_ResetBits(RelayOutCfgTable[i].GPIOx, RelayOutCfgTable[i].GPIO_Pin);
        else
            GPIO_SetBits(RelayOutCfgTable[i].GPIOx, RelayOutCfgTable[i].GPIO_Pin);
      #else
        if (TempOutSta & DEF_BIT_00)
            GPIO_SetBits(RelayOutCfgTable[i].GPIOx, RelayOutCfgTable[i].GPIO_Pin);
        else
            GPIO_ResetBits(RelayOutCfgTable[i].GPIOx, RelayOutCfgTable[i].GPIO_Pin);
      #endif

        TempOutSta >>= 1;
    }
    LastRelayOutputSta = RelayOutputSta;
}


/**
 * @brief 25ms
 * 
 * @param p_arg 
 */
void  App_Task_RELAY (void *p_arg)
{
  uint8_t  os_err;
  OS_FLAGS os_value;
  
  (void)p_arg;
  
  
  /* RAM Reset */
  PORLocalRAMReset_RELAY();
  
  /* HW Interface Initial */
  RelayOutCfg_Init();

  /* Status initial */
  PORInputsStaReset_RELAY();
  
  OSTimeDlyHMSM(0, 0, 0, 200);
  
  /* Display control */
 	while (DEF_TRUE) 
	{
    /*  Wait for State Update output Flags  */
    os_value = OSFlagPend(App_Flags_RELAY,
                          OSRelayFlags_ALL,
                          OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
                          0,
                         &os_err);
    if(os_err!=OS_ERR_NONE)
      SYS_DebugERROR;                                       /* Error- Only for Debug */
    
	  /* Relay Output*/
	  RelayOutputsLayerHandle(os_value);
	}
}


#endif